#!/usr/bin/python
# -*- coding: utf-8 -*-

"""Project Euler Solution 073

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import cProfile
import math
from fractions import gcd

def get_answer():
    """Question:
    
    Consider the fraction, n/d, where n and d are positive integers. If n<d and
    HCF(n,d)=1, it is called a reduced proper fraction.

    If we list the set of reduced proper fractions for d ≤ 8 in ascending order
    of size, we get:
    
    1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3,
    5/7, 3/4, 4/5, 5/6, 6/7, 7/8
    
    It can be seen that there are 3 fractions between 1/3 and 1/2.
    
    How many fractions lie between 1/3 and 1/2 in the sorted set of reduced 
    proper fractions for d ≤ 12,000?
    
    Note: The upper limit has been changed recently.
    """
    
    #The numerator of the highest fraction.
    numerator_to = 1.0
    
    #The denominator of the highest fraction.
    denominator_to = 2.0
    
    #The numerator of the lowest fraction.
    numerator_from = 1.0
    
    #The denominator of the lowest fraction.
    denominator_from = 3.0
    
    #The number of proper fractions with denominator <= 12,000, which fall
    #between 1/3 and 1/2.
    count = 0
    
    #The current denominator which is being tested.
    current_denominator = 4.0
    
    #Go through all the denominators which are less than or equal to 12,000
    #and count the number of proper fractions with that base which fall 
    #between 1/3 and 1/2.
    while current_denominator <= 12000:
        #The lowest numerator which can be tested for this denominator.
        #i.e. The numerator of 1/2 with this denominator as its base.
        numerator_range_from = int(
                                   math.ceil(
                                             current_denominator 
                                                / denominator_from
                                            ) * numerator_from
                                )
                            
        #The highest numerator which can be tested for this denominator.
        #i.e. The numerator of 1/2 with this denominator as its base.
        numerator_range_to = int(
                                 math.floor(
                                            current_denominator 
                                                / denominator_to
                                            ) * numerator_to
                                )
        
        #Count the number of proper fractions for this denominator, 
        #which fall between 1/3 and 1/2
        for i in xrange(numerator_range_from, numerator_range_to + 1):
            if gcd(i, current_denominator) == 1:
                count += 1
    
        #Move to the next denominator.
        current_denominator += 1
        
    return count
    
    

if __name__ == "__main__":
    cProfile.run("print(get_answer())")
